#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QPaintEvent>
#include "qpainter.h"
#include "qscreen.h"
#include "qguiapplication.h"
#include "qtimer.h"
#include "qmap.h"
#include "qdebug.h"
#include "qbytearray.h"
#include "QHBoxLayout"
#include "QLineEdit"
#include "QPushButton"

extern int xs;
extern int ys;
extern int ww;
extern int hh;
extern int gap;
extern int bigger;
extern int drawco ;

namespace Ui {
class MainWindow;
}

class MainWindow2 : public QWidget
{
    Q_OBJECT

public:
    QLineEdit* l1;
    QLineEdit* l2;
    QLineEdit* l3;
    QLineEdit* l4;

    QLineEdit* l_gap;
    QLineEdit* l_bigger;
    QLineEdit* l_co;

    explicit MainWindow2(QWidget *parent = nullptr)
    {
        this->setAttribute( Qt::WA_DeleteOnClose );
        QHBoxLayout *h = new QHBoxLayout();


        QVBoxLayout *v = new QVBoxLayout();
        v->addLayout(h);

        l1 = new QLineEdit(QString::number( xs));
        l2 = new QLineEdit(QString::number( ys));
        l3 = new QLineEdit(QString::number( ww));
        l4 = new QLineEdit(QString::number( hh));
        h->addWidget(l1);
        h->addWidget(l2);
        h->addWidget(l3);
        h->addWidget(l4);

        l_gap = new QLineEdit(QString::number( gap));
        v->addWidget(l_gap);
        l_bigger = new QLineEdit(QString::number( bigger));
        v->addWidget(l_bigger);

        auto qb = new QPushButton("Se&t");
        v->addWidget(qb);
        connect(qb, SIGNAL(clicked()), this, SLOT(onCli()));
        l_co = new QLineEdit(QString::number( 0));
        v->addWidget(l_co);
        this->setLayout(v);
        QTimer *timer = new QTimer(this);
        connect(timer, SIGNAL(timeout()), this, SLOT(onT()));
        timer->start(1000);

    }
public slots:
    void onCli()
    {
        xs =  l1->text().toInt();
        ys =  l2->text().toInt();
        ww =  l3->text().toInt();
        hh =  l4->text().toInt();
        gap =  l_gap->text().toInt();
        bigger =  l_bigger->text().toInt();

        if(ww*bigger>1366)
        {
            ww = 1366/bigger;
        }
        if(hh*bigger>768)
        {
            hh=768/bigger;
        }
    }

    void onT()
    {
        l_co->setText(QString::number( drawco));
    }
};
class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    int lastx=0,lasty=0;
    QByteArray lastimg;
    QImage lastDrawIm ;


    explicit MainWindow(QWidget *parent = nullptr)
    {
        this->setAttribute( Qt::WA_DeleteOnClose );
        this->setWindowFlags(Qt::WindowStaysOnTopHint);
        QTimer *timer = new QTimer(this);
        connect(timer, SIGNAL(timeout()), this, SLOT(onT()));
        timer->start(80);
        lastimg.resize(3000*3000);
    }
    ~MainWindow()
    {}


    void  paintEvent(QPaintEvent* e)
    {
        QWidget::paintEvent(e);
        QPainter painter(this);
        qDebug()<<"draw "<<drawco++;
        painter.drawImage(QRect(0,0,ww*bigger,hh*bigger),lastDrawIm);
    }

    void set_bigger_pix(int i,int j,bool isBlack)
    {
        for(int ii=i*bigger;ii<(i+1)*bigger;ii++)
        {
            for(int jj=j*bigger;jj<(j+1)*bigger;jj++)
            {
                if(isBlack)
                {
                    lastDrawIm.setPixelColor(ii,jj,Qt::black);
                }
                else
                {
                    lastDrawIm.setPixelColor(ii,jj,Qt::white);
                }
            }
        }
    }

public slots:
    void onT()
    {
        QScreen *screen  =  QGuiApplication::primaryScreen();
        auto im=screen->grabWindow(0,xs,ys,ww,hh).toImage();

        QPainter painter(&im);
        QPoint pos = QCursor::pos();
        auto cx = pos.x()-xs;
        auto cy = pos.y()-ys;
        auto pen = QPen();
        pen.setColor(Qt::black);
        pen.setWidth(2);
        painter.setPen(pen);
        painter.drawRect(cx,cy,10,10);
        pen.setColor(Qt::white);
        pen.setWidth(2);
        painter.setPen(pen);
        painter.drawRect(cx+2,cy+2,6,6);
        painter.end();

        lastDrawIm = QPixmap(QSize(ww*bigger,hh*bigger)).toImage();
        bool todraw = false;
        for (int i=0;i<ww;i++)
        {
            for(int j=0;j<hh;j++)
            {
                auto argb = im.pixel(i,j);

                int xx = qRed(argb)+
                        qGreen(argb)+
                        qBlue(argb);
                if(xx>gap)
                {
                    set_bigger_pix(i,j,false);
                    if(lastimg[i*hh+j]!=1)
                    {
                        todraw = true;
                    }
                    lastimg[i*hh+j] = 1;
                }
                else
                {
                    set_bigger_pix(i,j,true);
                    if(lastimg[i*hh+j]!=0)
                    {
                        todraw = true;
                    }
                    lastimg[i*hh+j] = 0;
                }
            }
        }
        if(!todraw)
        {
            return;
        }
        update();
    }
private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H
